package com.yh.csx.crm.service;

import com.yh.csx.crm.dao.SysProvinceMapper;
import com.yh.csx.crm.po.SysProvince;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author Huang Zhaoping
 * @date 2019/3/12
 */
@Service
public class ProvinceService {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private SysProvinceMapper provinceMapper;

    public List<SysProvince> getProvinceList() {
        return cacheService.getValue("ProvinceService.getProvinceList", 3600000, () -> provinceMapper.selectAllList());
    }

    public SysProvince getProvince(String code) {
        Optional<SysProvince> optional = getProvinceList().stream().filter(p -> Objects.equals(code, p.getProvinceCode())).findFirst();
        return optional.isPresent() ? optional.get() : null;
    }

    public String getProvinceName(String code) {
        SysProvince province = getProvince(code);
        return province == null ? null : province.getProvince();
    }
}
